package com.wenda.homeschooedu.daoimpl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.wenda.homeschooedu.DB.DB;
import com.wenda.homeschooedu.DB.JDBCConnection;
import com.wenda.homeschooedu.dao.Iduty;
import com.wenda.homeschooedu.model.Student;

public class DutyService implements Iduty {
	private JDBCConnection jdbc;
	private Connection connection;
	private Statement statement;

	public DutyService() {
		this.jdbc = new JDBCConnection();
		this.connection = jdbc.getConn();
		this.statement = jdbc.getStm();
	}

	public List<HashMap<Integer, String>> getDutyListString(int class_id ,int year , int term  , int weekNumber) {
		String sql = String.format(DB.Table.Duty.SQL.SELECT, class_id,year,term,weekNumber);
		try {
			ResultSet resultSet = statement.executeQuery(sql);
			List<HashMap<Integer, String>> list = new ArrayList<HashMap<Integer, String>>();
			if (resultSet != null) {
				int flag = 1;
				HashMap<Integer, String> weekOrderMap = new HashMap<Integer, String>();
				String names = ""; 
				while (resultSet.next()) {
					int day = resultSet.getInt(1);
					if (day == flag) {
						names = names+resultSet.getString(5)+" ";
					} else {
						weekOrderMap.put(flag, names);
						flag = day;
						names = resultSet.getString(5)+" ";
					}
				}
				weekOrderMap.put(flag, names);
				list.add(weekOrderMap);
			}

			return list;

		} catch (SQLException e) {
			// TODO Auto-generated catch block

			e.printStackTrace();
			return null;
		} finally {
			try {
				statement.close();

				connection.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	public List<HashMap<Integer, Student[]>> getDutyList() {

		return null;
	}

}
